/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once

#include <string>
#include <vector>

#include "../render/Image.h"
#include "../shared/geom.h"
#include "../shared/vector.h"
#include "Typedefs.h"

template<class T>
class QuadTree;
class IRender;
class ICamera;
class IEntity;
class Tile;

/**
======================================
A scene is a chunk of the map
There are many scenes which make up 
an entire map
======================================
*/
class Scene
{
public:
    Scene(void);

    /** Init */
    void Init(type_Layer one, type_Layer two);
	/** Draw the scene */
    void Draw( IRender *r, ICamera* cam );
	/** Free the scene - delete resources */
    void FreeScene();
    /** Test to see if the scene is loading */
    static bool IsLoading() { return m_loading; };
    /** Set to loading */
    void Loading(bool b) { m_loading = b; };

    /** Get the tile at this location in array*/
    Tile* GetTile(int layer, int x, int y);
    /** Get the tile from screen coords */
    Tile* GetScreenTile(int layer, int x, int y);

    /** Test wether entities are allowed on the tile */
    bool  Collide(int x, int y, Rect& bounds) const;
    /** Check the map boundries */
    bool  CheckBounds(int x, int y) const;
    
    /** Check for a Trigger event */
    void  InvokeTrigger( IEntity *ent, int x, int y ) const;

	/** Get the map X for tile retrieval */
    float GetX() const { return m_offset.x; };
    /** Get the map Y for tile retrieval */
	float GetY() const { return m_offset.y; };

    /** Set the max dimensions of the map */
    void SetMaxX(int mx) { m_maxX = mx; };
    void SetMaxY(int my) { m_maxY = my; };
    /** Get the Map dimensions */
    int GetMaxX() const { return m_maxX; };
    int GetMaxY() const { return m_maxY; };

    /** Center the camera around our player */
    void CenterCamera( ICamera* cam, Vector2f &pos ) const;

    /** convert from world coordinates to tile locations */
    void WorldToTile( int x, int y, int &px, int &py ) const;
    /** Convert Tile coordinates to world coordinates */
    void TileToWorld( int tx, int ty, int &wx, int &wy ) const;

    /** Get the background tiles */
    type_Layer &GetBackgroundLayer() { return m_layer1; };
    /** Get the foreground tiles */
    type_Layer &GetForegroundLayer() { return m_layer2; };

    
    struct tile_t   // these are individual tiles
    {
        Rect        m_bounds;
        Image       image;
        Vector2f    m_vPosition;
        unsigned int m_id;
        unsigned int GetID() { return m_id; };
        std::string GetName() { return ""; };
        Rect    GetBounds() { return m_bounds; };
        Vector2f GetLocation() { return m_vPosition; };
    };
    typedef std::vector< tile_t* > type_Tiles;

    /** Ray Tracer - Returns a list of tiles it collides with */
    void RayTrace( const Vector2f &start, const Vector2f &end, type_Tiles &tiles ) const;
    /** Retrieve Collidable Terrain data */
    QuadTree<Scene::tile_t*>* GetSecondLayerQuadTree() const { return m_secondLayerQuadTree; };
private:
    // check if we are indoors
    bool        m_inDoors;

    
	// if this scene has been created
    bool m_init;
	// max scene dimensions
    int m_maxX;
    int m_maxY;

	// current tile location
    Vector2f    m_offset;

    // layer one tiles
    type_Layer  m_layer1;
    // layer two tiles
    type_Layer  m_layer2;
    type_Tiles  m_tileReferences;

	// Standard tile size
	int         m_tileSize;

    /** Holds 2nd layer collision tiles */
    QuadTree<Scene::tile_t*>* m_secondLayerQuadTree;

    // if a new scene is being loaded
    static bool m_loading;
public:
    virtual ~Scene(void);
};
